using System;
using System.Collections.Generic;
using Entitas;

public sealed class CheckHealthSystem : ReactiveSystem {

    Context context;

    public CheckHealthSystem(Contexts contexts) : base(contexts.core)
    {
        context = contexts.core;
    }

    protected override void Execute(List<Entity> entities) {
        foreach(var e in entities) {
            if(e.health.value <= 0) {
                e.flagDestroy = true;
                if (e.isEnemy)
                {
                    Entity player = context.GetEntityWithPlayerId("Player1");
                    var before = player.hasStatistics ? player.statistics.score : 0;
                    player.ReplaceStatistics(before + 1);
                }
            }
        }
    }

    protected override Collector GetTrigger(Context context)
    {
        return context.CreateCollector(CoreMatcher.Health);
    }

    protected override bool Filter(Entity entity)
    {
        return true;
    }
}
